using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class ColorHelper : NodeHelper
    {
        public bool text;
        public bool image;
        public bool pro;
        public bool raw;
        public string type;
        private Image _Image;
        private RawImage _RawImage;
        private Text _Text;
        private TextMeshProUGUI _TextPro;

        protected override BindDataType bindDataType => BindDataType.String;

        private Text Text
        {
            get
            {
                if (_Text == null) _Text = GetComponent<Text>();
                return _Text;
            }
        }

        private TextMeshProUGUI TextPro
        {
            get
            {
                if (_TextPro == null) _TextPro = GetComponent<TextMeshProUGUI>();
                return _TextPro;
            }
        }

        private Image Image
        {
            get
            {
                if (_Image == null) _Image = GetComponent<Image>();
                return _Image;
            }
        }

        private RawImage RawImage
        {
            get
            {
                if (_RawImage == null) _RawImage = GetComponent<RawImage>();
                return _RawImage;
            }
        }

        protected override void BindData(string s)
        {
            SetColor(ColorTools.Get(type, s));
        }

        void SetColor(Color color)
        {
            if (text && !pro) Text.color = color;
            if (text && pro) TextPro.color = color;
            if (image && !raw) Image.color = color;
            if (image && raw) RawImage.color = color;
        }
    }
}